//
//  AttributedString+Extension.swift
//  Swift-Tools
//
//  Created by 深圳-周星驰 on 2020/10/15.
//

import Foundation
import UIKit

extension NSMutableAttributedString {
    /// 设置两种样式的富文本
    /// - Parameters:
    ///     - totalString: 所有文字
    ///     - normalColor: 默认的颜色
    ///     - normalFont:  默认的UIFont
    ///     - selectedString: 要设置的字体
    ///     - selectedColor:  要设置的字体颜色
    ///     - selectedFont:   要设置的字体大小
    static func setTwoStateText(_ totalString: String,
                                normalColor: UIColor,
                                normalFontSize: CGFloat,
                                selectedString: String,
                                selectedColor: UIColor,
                                selectedFontSize: CGFloat) -> NSMutableAttributedString {
        let attributedString = NSMutableAttributedString()
        let changeString = totalString.components(separatedBy: selectedString)
        let selectedDict = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: selectedFontSize),
            NSAttributedString.Key.foregroundColor: selectedColor
        ]
        let normalDict = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: normalFontSize),
            NSAttributedString.Key.foregroundColor: normalColor
        ]
        for i in 0..<changeString.count {
            let item = changeString[i]
            let normalContent = NSAttributedString(string: item, attributes: normalDict)
            attributedString.append(normalContent)
            
            if i != changeString.count - 1 {
                let selectContent = NSAttributedString(string: selectedString, attributes: selectedDict)
                attributedString.append(selectContent)
            }
        }
        
        return attributedString
    }
}
